package controller.v2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import model.LogOrderBack;
import model.MemberVO;
import model.OrderBackVO;
import model.OrderVO;
import model.UorderGoodVO;
import model.UorderVO;
import service.v2.ApplyReturnService;
import service.v2.MemberService;
import service.v2.OrderEvaluateService;
import service.v2.OrderService;
import service.v2.UorderBackService;
import service.v2.UorderService;
import util.PackageUtil;

@Controller
@RequestMapping("/v2/uorderback")
public class UorderBackController {
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private UorderService uorderService;
	@Autowired
	private UorderBackService uorderBackService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private ApplyReturnService applyReturnService;
	@Autowired
	private OrderEvaluateService orderEvaluateService;
	
	/**
	 * 退货列表
	 */
	@ResponseBody
	@RequestMapping(value="/getBackOrderList",method=RequestMethod.GET) 
	public Object getOrderList(@RequestHeader (value="LANG", required=false)String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,
			MemberVO memberVO,UorderVO ordervo) throws Exception {
			String country = null;
			if(lang==null){
				country = "1";
			}else{
				country = "0";
			}
			List<UorderVO> orderList = null;
			ordervo.setCreate_by(member_id);
			ordervo.setCountry(country);
			int total = uorderBackService.getTotal(ordervo);
			ordervo.setPagination(ordervo.getPage_index(), total + "");
			orderList = uorderBackService.getUorderBackList(ordervo);
			Map map = new HashMap();
			map.put("pagination", ordervo);
			map.put("list", orderList);
		return PackageUtil.packageParams(200, "success", map, "");
	}
	
	/**
	 * 退货详情
	 * @param case_id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/getBackOrderDetails",method=RequestMethod.GET) 
	public Object getBackOrderDetails(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,
			MemberVO memberVO,String case_id) throws Exception {
			if(case_id==null||"".equals(case_id) ){
				return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
			}
			UorderVO order = uorderBackService.getOrderAddressByCaseId(case_id);
			List<UorderGoodVO> list = uorderBackService.getBackOrderDetails(case_id);
			Map map  =  new HashMap();
			map.put("address_tel", order.getAddress_tel()==null?"":order.getAddress_tel());
			map.put("address_name", order.getAddress_name()==null?"":order.getAddress_name());
			map.put("address_content", order.getAddress_content()==null?"":order.getAddress_content());
			map.put("data", list);  
		return PackageUtil.packageParams(200, "success", map, "");
	}
	
	
	/**
	 * 提交退货申请
	 * @param token
	 * @param member_id
	 * @param order_id
	 * @param address_name
	 * @param address_tel
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/subOrderBack",method=RequestMethod.POST) 
	public Object subOrderBack(@RequestHeader (value="AUTHORIZATION", required=false) String token,HttpServletRequest request,@RequestHeader (value="SECRET", required=false)String member_id,
			String order_id,String address_name,String address_tel,String case_id,
			@RequestHeader(value="LANG", required=false)String lang) throws Exception {
		String country = "";
		if(lang==null){
			country = "1";
		}else{
			country = "0";
		}
		if(order_id==null||"".equals(order_id)||address_name==null||"".equals(address_name)||address_tel==null||"".equals(address_tel)||case_id==null||"".equals(case_id) ){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		boolean state1 = orderService.judgeState(order_id,"4");
		boolean state2 = orderService.judgeState(order_id,"3");
		if(state1==false&&state2==false){
			return PackageUtil.packageParams(10906,"订单状态异常","",""); 
		}
		MemberVO member = memberService.getAllInfoById(member_id);
		String data = request.getParameter("data");
		String order_back_id = uorderBackService.subOrderBack(member,data,order_id,case_id,address_name,address_tel,country);
		Map map = new HashMap();
		map.put("order_back_id", order_back_id);
		return PackageUtil.packageParams(200, "success", map, "");
	}
	
	
	/**
	 *  用户取消 申请退货
	 * @param token
	 * @param request
	 * @param member_id
	 * @param log_order_back
	 * @param order_id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/revokeOrderBack",method=RequestMethod.POST) 
	public Object revokeOrderBack(@RequestHeader (value="AUTHORIZATION", required=false) String token,HttpServletRequest request,@RequestHeader (value="SECRET", required=false)String member_id,
			String order_back_id,String order_id,String case_id,
			@RequestHeader(value="LANG", required=false)String lang) throws Exception {
		String country = "";
		if(lang==null){
			country = "1";
		}else{
			country = "0";
		}
		if(order_back_id==null||"".equals(order_back_id)||order_id==null||"".equals(order_id)||case_id==null||"".equals(case_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		System.out.println("***************************");
		boolean state1 = orderService.judgeState(order_id,"4");  
		boolean state2 = orderService.judgeState(order_id,"3");      
		if(state1==false&&state2==false){
			return PackageUtil.packageParams(10906,"订单状态异常","",""); 
		}
		boolean judge1 = applyReturnService.judgeOrderBackState(order_back_id,"0");
		boolean judge2 = applyReturnService.judgeOrderBackState(order_back_id,"1");
		
		if(judge1==false&&judge2==false){
			return PackageUtil.packageParams(10911,"退货订单异常","",""); 
		}
		uorderBackService.revokeOrderBack(order_id,order_back_id,case_id,member_id,country);
		return PackageUtil.packageParams(200, "success", "", "");
	}
	
	
	/**
	 * 查询退货LOG
	 * @param order_back_id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/getOrderCaseBackLog",method=RequestMethod.GET) 
	public Object getOrderCaseBackLog(@RequestHeader (value="AUTHORIZATION", required=false) String token,HttpServletRequest request,@RequestHeader (value="SECRET", required=false)String member_id,
			String order_back_id) throws Exception {
		if(order_back_id==null||"".equals(order_back_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		String state = "";
		OrderBackVO orderback  = applyReturnService.getOrderBackByOrderId(order_back_id);
		if(orderback!=null){
			state = orderback.getState();
		}
			List<LogOrderBack> logOrderBackList = applyReturnService.getLogOrderBackByOrderBackId(order_back_id);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("data", logOrderBackList);
			map.put("state", state);
			map.put("order_id", orderback.getOrder_id());
				
		return PackageUtil.packageParams(200,"successs",map,"");
	}
	
	
	/**
	 *  查看退货详情单
	 * @param token
	 * @param member_id
	 * @param memberVO
	 * @param order_back_id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getReturnDetails",method=RequestMethod.GET) 
	public Object checkRetDetail(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,
			MemberVO memberVO,String order_back_id){
		if(order_back_id==null||"".equals(order_back_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		OrderBackVO orderBackList = applyReturnService.getOrderBackByOrderId(order_back_id);
		return PackageUtil.packageParams(200,"successs",orderBackList,"");
	}
	
	
	/**
	 * 用户寄回退货商品
	 * @param token
	 * @param member_id
	 * @param order_back_id
	 * @param order_id
	 * @param ems_id
	 * @param ems_no
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/sendBackGoods",method=RequestMethod.POST) 
	public Object sendBackGoods(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,
			String order_back_id,String ems_id,String ems_no,
			@RequestHeader(value="LANG", required=false)String lang){
		String country = "";
		if(lang==null){
			country = "1";
		}else{
			country = "0";
		}
		if(order_back_id==null||"".equals(order_back_id)||ems_id==null||"".equals(ems_id)||ems_no==null||"".equals(ems_no)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		boolean judge = applyReturnService.judgeOrderBackState(order_back_id,"1");
		if(judge==false){
			return PackageUtil.packageParams(10911,"退货订单异常","",""); 
		}
		OrderBackVO order_back = uorderBackService.getorderbackById(order_back_id);
		if(order_back==null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		uorderBackService.sendBackGoods(member_id,order_back,ems_id,ems_no,country);
		return PackageUtil.packageParams(200,"successs","","");
	}
	
	
	/**
	 *  售后记录
	 * @param token
	 * @param member_id
	 * @param memberVO
	 * @param order_id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/afterSaleRecord",method=RequestMethod.GET) 
	public Object afterSaleRecord(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,
			MemberVO memberVO,String order_id,@RequestHeader(value="LANG", required=false)String lang){
		String country = "";
		if(lang==null){
			country = "1";
		}else{
			country = "0";
		}
		List<OrderBackVO> orderGoodList  =  orderEvaluateService.getOrderBackByMemberId(member_id,order_id,country);
		return PackageUtil.packageParams(200,"successs",orderGoodList,"");
	}
	
}
